package cn.stard.pet.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.exception.BusinessException;
import cn.stard.basic.util.LoginContext;
import cn.stard.org.domain.Employee;
import cn.stard.org.service.IEmployeeservice;
import cn.stard.pet.service.IPetService;
import cn.stard.pet.domain.Pet;
import cn.stard.pet.query.PetQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import cn.stard.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private IEmployeeservice employeeService;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name="宠物基本信息添加修改", sn = "pet:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest req){
        try {
            // 通过工具类来获取登录用户id
            Logininfo Logininfo = LoginContext.getLogininfo(req);
            // 通过用户id来获取用户对象
            Employee employee = employeeService.loadByloginfoId(Logininfo.getId());
            // 通过用户获取到店铺id
            Long shopId = employee.getShopId();
            // 把店铺id添加到里面
            pet.setShopId(shopId);
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name="宠物基本信息删除", sn = "pet:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name="宠物基本信息根据id查询", sn = "pet:get")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name="宠物基本信息集合", sn = "pet:list")
    public List<Pet> list(){

        return petService.loadAll();
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: offsale
     * @Description: 宠物批量下架
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/7/16 15:27
     * @Parameters: [ids]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name="宠物基本信息分页查询", sn = "pet:queryData")
    public PageList<Pet> queryData(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }
}
